package com.demo.project_template.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.project_template.base.pay.wxpay.service.WechatJSAPIGetMsgService;
import com.demo.project_template.base.pay.wxpay.vo.DecryptWeChartMsgVO;
import com.demo.project_template.constant.AuthenticationConstants;
import com.demo.project_template.entity.Friend;
import com.demo.project_template.entity.Gift;
import com.demo.project_template.entity.User;
import com.demo.project_template.entity.UserGift;
import com.demo.project_template.exception.ErrorCodeException;
import com.demo.project_template.mapper.FriendMapper;
import com.demo.project_template.mapper.GiftMapper;
import com.demo.project_template.mapper.UserMapper;
import com.demo.project_template.service.UserGiftService;
import com.demo.project_template.service.UserGoldRecordService;
import com.demo.project_template.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.util.CheckEmptyUtil;
import com.demo.project_template.util.RedisUtil;
import com.demo.project_template.vo.request.user.MatchingVO;
import com.demo.project_template.vo.response.LoginResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2023-06-19 15:56:44
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserGiftService userGiftService;
    @Resource
    private GiftMapper giftMapper;
    @Resource
    private FriendMapper friendMapper;

    @Override
    public User matching(MatchingVO matchingVO) {
        if (!"3".equals(matchingVO.getType()) && !"2".equals(matchingVO.getType()) && !"1".equals(matchingVO.getType())){
            throw new ErrorCodeException("参数错误");
        }
        User user;
        long count = count(new LambdaQueryWrapper<User>().eq(User::getGender,matchingVO.getGender())
                .eq("3".equals(matchingVO.getType()),User::getConstellation, matchingVO.getConstellation())
                .eq("2".equals(matchingVO.getType()),User::getHobbiesJson,matchingVO.getHobbies())
                .eq("1".equals(matchingVO.getType()),User::getNature, matchingVO.getNature()));
        if (count>0){
            long offset = (long) (Math.random() * count);
            List<User> users = list(new LambdaQueryWrapper<User>()
                    .eq(User::getGender,matchingVO.getGender())
                    .eq("3".equals(matchingVO.getType()),User::getConstellation, matchingVO.getConstellation())
                    .eq("2".equals(matchingVO.getType()),User::getHobbiesJson,matchingVO.getHobbies())
                    .eq("1".equals(matchingVO.getType()),User::getNature, matchingVO.getNature())
                    .last("limit " + offset + ",1"));
            user = users.get(0);
        }else {
            List<User> users = list(new LambdaQueryWrapper<User>()
                    .eq(User::getGender, matchingVO.getGender()));
            if (users.isEmpty()){
                return null;
            }
            long index = (long) (Math.random() * users.size());
            user = users.get((int) index);
        }
        return user;
    }

    @Override
    public String getUserPicture(Long manUserId, Long womanUserId, Integer giftId) {
        //判断男用户是否为会员
        User man = getById(manUserId);
        if ("1".equals(man.getIsMember())){
            User woman = getById(womanUserId);
            return woman.getPicturesJson();
        }

        //判断男用户和女用户是否为畅聊好友
        Friend friend = friendMapper.selectOne(new LambdaQueryWrapper<Friend>().eq(Friend::getManId, manUserId).eq(Friend::getWomanId, womanUserId));
        if (null != friend){
            User woman = getById(womanUserId);
            return woman.getPicturesJson();
        }

        Gift gift = giftMapper.selectById(giftId);
        if (!"4".equals(gift.getType())){
            throw new ErrorCodeException("送的礼物不支持解锁相册");
        }

        //普通用户则向女性送礼物
        UserGift userGift = new UserGift();
        userGift.setUserId(womanUserId);
        userGift.setSendUserId(manUserId);
        userGift.setGiftId(giftId);
        userGiftService.sendGift(userGift);

        //返回女用户生活照
        User woman = getById(womanUserId);
        return woman.getPicturesJson();
    }
}
